package com.lyshop.guiyingshanding.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

import com.lyshop.guiyingshanding.R;


/**
 * Created by Mr on 2018/10/17.
 */

public class AmountGroup extends RelativeLayout implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private RadioGroup rg_short;
    private RadioGroup rg_top;
    private RadioGroup rg_bottom;
    private AmountGroupListener mListener;
    private Context mContext;
    private boolean isDark;
    private int mCheckId;

    public AmountGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_amount_group, this, true);
        findViews(view);
        setListeners();
    }

    private void findViews(View view) {
        rg_short = (RadioGroup) view.findViewById(R.id.rg_short);
        rg_top = (RadioGroup) view.findViewById(R.id.rg_top);
        rg_bottom = (RadioGroup) view.findViewById(R.id.rg_bottom);
    }

    private void setListeners() {
        rg_short.setOnCheckedChangeListener(this);
        rg_top.setOnCheckedChangeListener(this);
        rg_bottom.setOnCheckedChangeListener(this);
    }

    public void setAmountGroupListener(AmountGroupListener listener) {
        mListener = listener;
    }

    public void init() {
        clearRadioGroup(rg_short);
        clearRadioGroup(rg_top);
        clearRadioGroup(rg_bottom);
        rg_short.setVisibility(VISIBLE);
        rg_top.setVisibility(GONE);
        rg_bottom.setVisibility(GONE);
        ((RadioButton) rg_short.getChildAt(0)).setChecked(true);
        setShortTextColor(rg_short.getChildAt(0).getId());
        setOpenTextColor(rg_top.getChildAt(0).getId());
    }

    public void setDark(boolean isDark){
        this.isDark = isDark;
    }

    public void setShortTextColor(int checkId) {
        for (int i = 0; i < rg_short.getChildCount(); i++) {
            if (rg_short.getChildAt(i).getId() == checkId) {
                ((MyRadioButton) rg_short.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.white));
                ((MyRadioButton) rg_short.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.white));
            } else {
                if(isDark){
                    ((MyRadioButton) rg_short.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    ((MyRadioButton) rg_short.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.white));
                }else{
                    ((MyRadioButton) rg_short.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.color_black_01));
                    ((MyRadioButton) rg_short.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.color_black_02));
                }
            }
        }
    }

    public void setOpenTextColor(int checkId) {
        for (int i = 0; i < rg_top.getChildCount(); i++) {
            if (rg_top.getChildAt(i).getId() == checkId) {
                ((MyRadioButton) rg_top.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.white));
                ((MyRadioButton) rg_top.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.white));
            } else {
                if(isDark){
                    ((MyRadioButton) rg_top.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    ((MyRadioButton) rg_top.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.white));
                }else{
                    ((MyRadioButton) rg_top.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.color_black_01));
                    ((MyRadioButton) rg_top.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.color_black_02));
                }}
        }

        for (int i = 0; i < rg_bottom.getChildCount(); i++) {
            if (rg_bottom.getChildAt(i).getId() == checkId) {
                ((MyRadioButton) rg_bottom.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.white));
                ((MyRadioButton) rg_bottom.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.white));
            } else {
                if(isDark){
                    ((MyRadioButton) rg_bottom.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    ((MyRadioButton) rg_bottom.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.white));
                }else{
                    ((MyRadioButton) rg_bottom.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.color_black_01));
                    ((MyRadioButton) rg_bottom.getChildAt(i)).setBigColor(ContextCompat.getColor(mContext, R.color.color_black_02));
                }}
        }
    }


    public void setBackgroundStyle(int background) {
        for (int i = 0; i < rg_short.getChildCount(); i++) {
            rg_short.getChildAt(i).setBackgroundResource(background);
        }
        for (int i = 0; i < rg_top.getChildCount(); i++) {
            rg_top.getChildAt(i).setBackgroundResource(background);
        }
        for (int i = 0; i < rg_bottom.getChildCount(); i++) {
            rg_bottom.getChildAt(i).setBackgroundResource(background);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_short:
                onShortChecked(checkedId);
                break;
            case R.id.rg_top:
                onTopChecked(checkedId);
                break;
            case R.id.rg_bottom:
                onBottomChecked(checkedId);
                break;
        }
    }

    private void onShortChecked(int checkedId) {
        switch (checkedId) {
            case R.id.rb_amount1:
                ((RadioButton) rg_top.getChildAt(0)).setChecked(true);
                break;
            case R.id.rb_amount2:
                ((RadioButton) rg_top.getChildAt(1)).setChecked(true);
                break;
            case R.id.rb_amount5:
                ((RadioButton) rg_top.getChildAt(4)).setChecked(true);
                break;
            case R.id.rb_amount10:
                ((RadioButton) rg_bottom.getChildAt(4)).setChecked(true);
                break;
            case R.id.rb_more:
                rg_short.setVisibility(GONE);
                rg_top.setVisibility(VISIBLE);
                rg_bottom.setVisibility(VISIBLE);
                break;
        }
        setShortTextColor(checkedId);
    }

    private void onTopChecked(int checkedId) {
        clearRadioGroup(rg_bottom);
        switch (checkedId) {
            case R.id.rb_1:
                mListener.onCheckedAmount(1);
                break;
            case R.id.rb_2:
                mListener.onCheckedAmount(2);
                break;
            case R.id.rb_3:
                mListener.onCheckedAmount(3);
                break;
            case R.id.rb_4:
                mListener.onCheckedAmount(4);
                break;
            case R.id.rb_5:
                mListener.onCheckedAmount(5);
                break;
        }
        setOpenTextColor(checkedId);
    }

    private void onBottomChecked(int checkedId) {
        clearRadioGroup(rg_top);
        switch (checkedId) {
            case R.id.rb_6:
                mListener.onCheckedAmount(6);
                break;
            case R.id.rb_7:
                mListener.onCheckedAmount(7);
                break;
            case R.id.rb_8:
                mListener.onCheckedAmount(8);
                break;
            case R.id.rb_9:
                mListener.onCheckedAmount(9);
                break;
            case R.id.rb_10:
                mListener.onCheckedAmount(10);
                break;
        }
        setOpenTextColor(checkedId);
    }

    private void clearRadioGroup(RadioGroup group) {
        group.setOnCheckedChangeListener(null);
        group.clearCheck();
        group.setOnCheckedChangeListener(this);
    }

    public interface AmountGroupListener {
        void onCheckedAmount(int amount);
    }
}
